package top.timous.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.timous.pojo.UserInfo;
import top.timous.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String login(UserInfo userInfo , Model model , HttpServletRequest request){
        // 判断是不是有我们的逻辑
        HttpSession session = request.getSession(true);
        System.out.println(userInfo);
        UserInfo loginedUser = userService.queryUserById(userInfo);
        if (loginedUser != null){
            session.setAttribute("loginedUser",loginedUser);
            return "redirect:/main.jsp";
        }
        model.addAttribute("msg" , "用户名或者密码错误！！");
        return "login";
    }


    @RequestMapping("/main")
    public String main(){
        return "main";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession(true);
//        session.setAttribute("loginedUser" , null);
        session.invalidate();
//        session.removeAttribute("loginedUser");
        return "redirect:/login.jsp";
    }


    @RequestMapping("/saveUser")
    @ResponseBody
    public Map<String, Object> saveUser(@RequestBody UserInfo userInfo){
        UserInfo userInfo1 = userService.queryUserByAccount(userInfo);
        System.out.println("=======================");
        System.out.println(userInfo);
        Map<String , Object> map = new HashMap();
        if (userInfo1 == null){
            //成功
            map.put("msg" , "注册成功");
            map.put("message" , 1);
            userService.saveUser(userInfo);
        }else{
            //失败
            map.put("msg" , "注册失败");
            map.put("message" , -1);
        }
        return map;
    }
    @RequestMapping("/updateUser")
    @ResponseBody
    public Map<String, Object> updateUser(@RequestBody UserInfo userInfo){
        boolean flag = true;
        // account 修改
        //存在 ， 但不是它本身
        UserInfo userInfo1 = userService.queryUserByAccount(userInfo);
        // account 不修改

        UserInfo userInfo2 = userService.queryUserById(userInfo);
        Map<String , Object> map = new HashMap();
        if (userInfo == null){
            map.put("msg" , "更新失败");
            map.put("message" , -1);
            return map;
        }
        if (userInfo1 != null){

            if (userInfo.getUserId() != userInfo1.getUserId()){
                //存在 ， 但不是它本身有使用这个account的用户   无法进行修改
                flag = false;
            }

        }

        if (flag){
            //能够修改成功
            map.put("msg" , "注册成功");
            map.put("message" , 1);
            userService.updateUser(userInfo);
        }else {
            //已经存在该用户了
            map.put("msg" , "更新失败");
            map.put("message" , -1);
        }
        return map;
    }


    @RequestMapping("/queryUserByAccount")
    @ResponseBody
    public Map<String , Object> queryUserByAccount(@RequestBody UserInfo userInfo){
        UserInfo userInfo1 = userService.queryUserByAccount(userInfo);
        Map<String , Object> map = new HashMap();
        if (userInfo1 == null){
            //  查询结果为空  查询失败
            map.put("message" , -1); // 查询失败
        }else{
            map.put("message" , 1);
            map.put("data" , userInfo1);
        }
        return map;
    }
    @RequestMapping("/deleteUserByAccount")
    @ResponseBody
    public Map<String , Object> deleteUserByAccount(@RequestBody UserInfo userInfo){
        UserInfo userInfo1 = userService.queryUserByAccount(userInfo);
        Map<String , Object> map = new HashMap();
        if (userInfo1 == null){
            //  查询结果为空  查询失败
            map.put("message" , -1); // 查询失败
        }else{
            map.put("message" , 1);
            userService.deleteUserByAccount(userInfo.getAccount());
        }
        return map;
    }

}
